// index.js
import Dialog from '@vant/weapp/dialog/dialog';
const app = getApp({})

Page({
    data:{
        swiperImgs:[
            "https://qcloud.dpfile.com/pc/9LKrXN14Tb8od-cs1Wgc6dDLklFD9tf9gCHR-AYX6e-how6pX0gWG-2v4NsvChrI.jpg",
            "https://img2.baidu.com/it/u=3169174963,3242219427&fm=253&fmt=auto&app=138&f=JPEG?w=570&h=380",
        ],
        announcementText:"",
        userCollect:[], // 我的收藏
        userLike:[], // 常去教室
        roomSearch:[], // 热门教室
    },

    // 获取教室推荐信息
    getRoomRecommendations() {
        const userInfo = wx.getStorageSync('userInfo');
        if (!userInfo || !userInfo.id) {
            wx.showToast({
                title: '请先登录',
                icon: 'none'
            });
            return;
        }

        wx.request({
            url: `${app.globalData.baseUrl}/admin/rooms/home-summary`,
            method: 'GET',
            data: {
                userId: userInfo.id
            },
            success: (res) => {
                console.log(res);
                if (res.data.code === 200) {
                    const { favoriteRooms, frequentRooms, hotRooms } = res.data.data;
                    this.setData({
                        userCollect: favoriteRooms.map(room => ({
                            id: room.roomId,
                            buildNumber: room.buildingName,
                            roomNumber: room.roomName
                        })),
                        userLike: frequentRooms.map(room => ({
                            id: room.roomId,
                            buildNumber: room.buildingName,
                            roomNumber: room.roomName
                        })),
                        roomSearch: hotRooms.map(room => ({
                            id: room.roomId,
                            buildNumber: room.buildingName,
                            roomNumber: room.roomName
                        }))
                    });
                } else {
                    wx.showToast({
                        title: '获取教室推荐信息失败',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                console.error('获取教室推荐信息失败:', err);
                wx.showToast({
                    title: '网络错误',
                    icon: 'none'
                });
            }
        });
    },

    /* AI教室 */
    NavgiateToRoom(){
        wx.navigateTo({
          url: '/pages/chatai/chatai',
        })
    },

    onLoad() {
        // 获取公告
        wx.request({
            url: app.globalData.baseUrl+'/admin/announcements',
            method: 'GET',
            success: (res) => {
                if (res.data.announcements && res.data.announcements.length > 0) {
                    const sortedAnnouncements = res.data.announcements.sort((a, b) => 
                        new Date(b.created_at) - new Date(a.created_at)
                    );
                    const latestAnnouncement = sortedAnnouncements[0];
                    this.setData({
                        announcementText: `${latestAnnouncement.title}: ${latestAnnouncement.content}`
                    });
                    Dialog.alert({
                        title: '公告',
                        message:`${latestAnnouncement.title}: ${latestAnnouncement.content}`,
                      }).then(() => {
                        // on close
                    });
                } else {
                    this.setData({
                        announcementText: '欢迎来到青岛工学院智能教室系统平台'
                    });
                }
            },
            fail: (err) => {
                console.error('获取公告失败:', err);
                this.setData({
                    announcementText: '欢迎来到青岛工学院智能教室系统平台'
                });
            },
        });

        // 获取教室推荐信息
        this.getRoomRecommendations();
    },

    /* 教师预约 */
    navPreOrderRoom(e) {
        const { buildingid, roomid } = e.currentTarget.dataset;
        wx.navigateTo({
            url: `/pages/preorderroom/preorderroom?buildingId=${buildingid}&roomId=${roomid}`,
        })
    },

    /*  */
    navPreSchdule(){
        wx.switchTab({
          url: '/pages/schedule/schedule',
        })
    },

    /* 空教室 */
    navToEmptyRoom(){
        wx.navigateTo({
          url: '/pages/emptyRoom/emptyRoom',
        })
    },

    NavToHistoryAnnocement(){
        wx.navigateTo({
          url: '/pages/historyannocument/historyannocument',
        })
    }
})